package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	// "bp/internal/util"
	"context"
)

// ListCouponPtrsAsOfferOfItemIDParam 根据商品列出作为优惠项目的优惠券参数
type ListCouponPtrsAsOfferOfItemIDParam struct {
	OwnerID int64
	Issuer  string
	ItemID  int64
}

// ListCouponPtrsAsOfferOfOwner 根据所有者列出作为优惠项目的优惠券
func (s *CouponEntityServiceImp) ListCouponPtrsAsOfferOfOwner(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponPtrsAsOfferOfItemIDParam) (couponPtrs []*sqlc.Coupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	// slog.Infof("cPtr=>%+v", *cPtr)
	coupons, err := q.ListAvailableCouponsByOwnerID(ctx, sqlc.ListAvailableCouponsByOwnerIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		Ts:      cPtr.PagerPtr.Ts,
		Offset:  cPtr.PagerPtr.Offset,
		Limit:   cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	couponPtrs = s.MapCouponEntityToPtr(coupons)
	return
}

// ListCouponPtrsAsOfferOfItemID 根据商品列出作为优惠项目的优惠券
func (s *CouponEntityServiceImp) ListCouponPtrsAsOfferOfItemID(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponPtrsAsOfferOfItemIDParam) (couponPtrs []*sqlc.Coupon, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	// slog.Infof("cPtr=>%+v", *cPtr)
	rowsMatchIncludeItemID, err := q.ListAvailableCouponsMatchIncludeItemID(ctx, sqlc.ListAvailableCouponsMatchIncludeItemIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		ItemID:  ptr.ItemID,
	})
	if err != nil {
		return
	}
	couponPtrs = append(couponPtrs, s.MapCouponEntityToPtr(rowsMatchIncludeItemID)...)
	rowsMatchExcludeItemID, err := q.ListAvailableCouponsMatchExcludeItemID(ctx, sqlc.ListAvailableCouponsMatchExcludeItemIDParams{
		BrandID: cPtr.BrandID,
		OwnerID: ptr.OwnerID,
		Issuer:  ptr.Issuer,
		ItemID:  ptr.ItemID,
	})
	if err != nil {
		return
	}
	couponPtrs = append(couponPtrs, s.MapCouponEntityToPtr(rowsMatchExcludeItemID)...)
	return
}
